﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using B4Deal.Models;

namespace B4Deal
{
    public class MyDataMembershipProvider : MembershipProvider
    {
        UserManager repository;

        public MyDataMembershipProvider()
        {
            repository = new UserManager();
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            if (!ValidateUser(username, oldPassword) || string.IsNullOrEmpty(newPassword.Trim()))
                return false;

            TAIKHOAN user = repository.GetUser(username);
           // string hash = FormsAuthentication.HashPasswordForStoringInConfigFile(newPassword.Trim(), "md5");

            user.Password = newPassword ;
            repository.Save();
            return true;
        }

        public void CreateUser(string email, string password, string name)
        {
            repository.CreateUser(email, name, password);
            repository.Save();
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            return repository.DeleteUser(username);
        }

        public override int MinRequiredPasswordLength
        {
            get { return 6; }
        }

        public override bool ValidateUser(string username, string password)
        {
            if (string.IsNullOrEmpty(password.Trim()) || string.IsNullOrEmpty(username.Trim()))
                return false;

            //string hash = FormsAuthentication.HashPasswordForStoringInConfigFile(password.Trim(), "md5");
            List<TAIKHOAN> lst = repository.GetAllUser();
            foreach (TAIKHOAN i in lst)
                if (i.Email.Trim().CompareTo(username.Trim()) == 0 && i.Password.Trim().CompareTo(password.Trim()) == 0)
                    return true;
            return false;
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            throw new NotImplementedException();
        }

        public override bool EnablePasswordReset
        {
            get { throw new NotImplementedException(); }
        }

        public override bool EnablePasswordRetrieval
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public TAIKHOAN GetUser(string email)
        {
            return repository.GetUser(email);
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override string GetUserNameByEmail(string email)
        {
            throw new NotImplementedException();
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { throw new NotImplementedException(); }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotImplementedException(); }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresUniqueEmail
        {
            get { throw new NotImplementedException(); }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public bool modifyUser(string email, DateTime ngaysinh, bool gioitinh, string phone, int Tinhtrang, int loaiTK)
        {
            TAIKHOAN tk = repository.GetUser(email);
            return true;
        }

        public bool modifyUser(string email, int d, int m, int y, bool gioitinh)
        {
            if (string.IsNullOrEmpty(email.Trim()))
                return false;

            TAIKHOAN tk = repository.GetUser(email);
            if (tk == null)
                throw new ArgumentException("Không tìm thấy tài khoản!");
            DateTime date = new DateTime(y, m, d);
            tk.NgaySinh = date;
            tk.GioiTinh = gioitinh;
            repository.Save();
            return true;
        }
    }
}